package com.ruicar.afs.cloud.apply.pre.loan.mq.sender.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ruicar.afs.cloud.apply.business.mq.config.ArchiveConfig;
import com.ruicar.afs.cloud.apply.business.service.ArchiveMQService;
import com.ruicar.afs.cloud.apply.common.entity.ApplyChannelInfo;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.pre.loan.mq.sender.service.ConditionApprovalMessageSenderSerevice;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyChannelInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ResultBooleanEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ConditionalApprovalReturnInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.parameter.commom.enums.AProveBusinessTypeEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;


/**
 * 附條件核准MQ報文推送
 *
 * @author 耿继辉
 */
@Service
@Slf4j
@AllArgsConstructor
public class ConditionApprovalMessageSenderSereviceImpl implements ConditionApprovalMessageSenderSerevice {
    private final ApplyChannelInfoService applyChannelInfoService;
    private final ApplyOrderInfoService applyOrderInfoService;
    private final ArchiveMQService archiveMQService;

    /**
     * 附條件核准報文組裝
     *
     * @param applyNo
     * @return
     * @throws ParseException
     */
    @Override
    public AfsTransEntity<ConditionalApprovalReturnInfo> returnMessage(String applyNo) throws ParseException {
        AfsTransEntity<ConditionalApprovalReturnInfo> entity = new AfsTransEntity<>();
        ConditionalApprovalReturnInfo conditionalApprovalReturnInfo = new ConditionalApprovalReturnInfo();
        ApplyOrderInfo orderInfoByApplyNo = applyOrderInfoService.getOrderInfoByApplyNo(applyNo);
        ApplyChannelInfo channelInfoByApplyNo = applyChannelInfoService.getChannelInfoByApplyNo(applyNo);
        if (ObjectUtil.isNotNull(orderInfoByApplyNo)) {
            //訂單狀態是核准
            if (ApplyConstants.APPLY_STATUS_PASS.equals(orderInfoByApplyNo.getApplyStatus())) {
                conditionalApprovalReturnInfo.setApplyNo(applyNo);
                conditionalApprovalReturnInfo.setApproveType(AProveBusinessTypeEnum.APPROVAL.getCode());
                conditionalApprovalReturnInfo.setResult(ResultBooleanEnum.TRUE.getCode());
                conditionalApprovalReturnInfo.setDealearName(channelInfoByApplyNo.getChannelName());
            } else if (ApplyConstants.APPLY_STATUS_REFUSE.equals(orderInfoByApplyNo.getApplyStatus())) {
                conditionalApprovalReturnInfo.setApplyNo(applyNo);
                conditionalApprovalReturnInfo.setApproveType(AProveBusinessTypeEnum.REJECTION.getCode());
                conditionalApprovalReturnInfo.setResult(ResultBooleanEnum.FALSE.getCode());
                conditionalApprovalReturnInfo.setDealearName(channelInfoByApplyNo.getChannelName());
                //拒绝附条件核准，推送订单废弃至归档，拒绝
                try {
                    archiveMQService.archivePushMQ(orderInfoByApplyNo.getId().toString(), null, applyNo, ArchiveConfig.wworder_refuse);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        entity.setData(conditionalApprovalReturnInfo);
        entity.setTransCode(MqTransCode.AFS_POS_CASE_APPLY_CTM_APPROVE_CONDITIONAL_NOTICE);
        return entity;
    }
}
